/*
 * Copyright (C) 2019 Peng fei Pan <panpfpanpf@outlook.me>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.panpf.sketch.process;


import me.panpf.sketch.Sketch;
import me.panpf.sketch.cache.BitmapPool;
import me.panpf.sketch.cache.BitmapPoolUtils;
import me.panpf.sketch.request.Resize;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.utils.TextTool;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;


/**
 * 用于组合两个 {@link ImageProcessor} 一起使用，可以无限嵌套
 */
@SuppressWarnings("WeakerAccess")
public abstract class WrappedImageProcessor extends ResizeImageProcessor {

    private WrappedImageProcessor wrappedProcessor;

    protected WrappedImageProcessor(WrappedImageProcessor wrappedProcessor) {
        this.wrappedProcessor = wrappedProcessor;
    }

    @Override
    public final PixmapBuild process(Sketch sketch, PixmapBuild bitmap, Resize resize, boolean lowQualityImage) {
        //noinspection ConstantConditions
        if (bitmap == null || bitmap.getPixelMap().isReleased()) {
            return bitmap;
        }

        // resize
        PixmapBuild newBitmap = bitmap;
        if (!isInterceptResize()) {
            newBitmap = super.process(sketch, bitmap, resize, lowQualityImage);
        }

        // wrapped
        if (wrappedProcessor != null) {
            PixmapBuild wrappedBitmap = wrappedProcessor.process(sketch, newBitmap, resize, lowQualityImage);
            if (wrappedBitmap != newBitmap) {
                if (newBitmap != bitmap) {
                    BitmapPool bitmapPool = sketch.getConfiguration().getBitmapPool();
                    BitmapPoolUtils.freeBitmapToPool(newBitmap, bitmapPool);
                }
                newBitmap = wrappedBitmap;
            }
        }
        return onProcess(sketch, newBitmap, resize, lowQualityImage);
    }

    public abstract PixmapBuild onProcess(Sketch sketch, PixmapBuild bitmap, Resize resize, boolean lowQualityImage);


    public WrappedImageProcessor getWrappedProcessor() {
        return wrappedProcessor;
    }

    @Override
    public String getKey() {
        String selfKey = onGetKey();
        String wrappedKey = wrappedProcessor != null ? wrappedProcessor.getKey() : null;

        if (!TextTool.isNullOrEmpty(selfKey)) {
            if (!TextTool.isNullOrEmpty(wrappedKey)) {
                return String.format("%s->%s", selfKey, wrappedKey);
            } else {
                return selfKey;
            }
        } else if (!TextTool.isNullOrEmpty(wrappedKey)) {
            return wrappedKey;
        }
        return null;
    }

    public abstract String onGetKey();

    @Override
    public String toString() {
        String selfToString = onToString();
        String wrappedToString = wrappedProcessor != null ? wrappedProcessor.toString() : null;

        if (TextTool.isNullOrEmpty(wrappedToString)) {
            return selfToString;
        }
        return String.format("%s->%s", selfToString, wrappedToString);
    }

    public abstract String onToString();

    protected boolean isInterceptResize() {
        return false;
    }
}
